﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenExpressApp.Template;
using Csla.Wpf;
using System.ComponentModel;
using System.Windows.Data;
using System.Windows;
using System.Windows.Controls;
using System.Diagnostics;
using OpenExpressApp.WPF.Controls;
using Itenso.Windows.Input;
using Csla;
using Csla.Core;
using OpenExpressApp.Module;
using OpenExpressApp.MetaModel;
using OpenExpressApp.Types;
using System.Windows.Controls.Primitives;
using AvalonDock;
using OpenExpressApp.WPF.Command;

namespace OpenExpressApp.Module.WPF.Templete
{
    public class ListFramController : IFrameController
    {

        private GCslaDataProvider _dpList;

        private Panel listRegion;

        private Object[] getListParam;

        public ListFramController(IFrameTemplate FrameTemplate, params Object[] getListParam)
        {
            this.getListParam = getListParam;
            this.FrameTemplate = FrameTemplate;

            listRegion = FrameTemplate.GetRegion(RegionNames.ListRegion) as Panel;
            //生成列表界面
            _dpList = new GCslaDataProvider();
            (_dpList as ISupportInitialize).BeginInit();
            _dpList.IsAsynchronous = true;
            _dpList.ManageObjectLifetime = false;
            _dpList.IsInitialLoadEnabled = false;
            _dpList.DataChanged += new EventHandler(_dpList_DataChanged);
            (_dpList as ISupportInitialize).EndInit();

            listRegion.Children.Add(View.Control as FrameworkElement);
            //生成busy控件
            BusyAnimation busy = new BusyAnimation
            {
                HorizontalAlignment = System.Windows.HorizontalAlignment.Center,
                Width = 20,
            };
            //绑定TextBox到对象属性
            Binding bdIsRunning = new Binding("IsBusy");
            bdIsRunning.Source = _dpList;
            bdIsRunning.BindsDirectlyToSource = true;

            busy.SetBinding(BusyAnimation.IsRunningProperty, bdIsRunning);
            listRegion.Children.Add(busy);
        }

 
        void _dpList_DataChanged(object sender, EventArgs e)
        {
            if (null != _dpList.Error) return;
            FrameTemplate.View.Data = _dpList.Data;
        }

        public ListObjectView View
        {
            get
            {
                return FrameTemplate.View as ListObjectView;
            }
        }

        public Type BOType
        {
            get
            {
                return FrameTemplate.View.BOType;
            }
        }

        #region IFrameController Members

        public IFrameTemplate FrameTemplate { get; set; }


        public void GetObject()
        {
            using (_dpList.DeferRefresh())
            {
                _dpList.ObjectType = BOType.Assembly.GetType(BOType.FullName + "List");
                if (null == _dpList.ObjectType) _dpList.ObjectType = BOType.Assembly.GetType(BOType.FullName + "s");
                _dpList.FactoryMethod = "GetList";
                _dpList.FactoryParameters.Clear();
                foreach(var item in getListParam)
                    _dpList.FactoryParameters.Add(item);
            }
        }

        public void GetObject(IQueryObject criteria)
        {
            using (_dpList.DeferRefresh())
            {
                _dpList.ObjectType = BOType.Assembly.GetType(BOType.FullName + "List");
                if (null == _dpList.ObjectType) _dpList.ObjectType = BOType.Assembly.GetType(BOType.FullName + "s");
                _dpList.FactoryMethod = "GetList";
                _dpList.FactoryParameters.Clear();
                _dpList.FactoryParameters.Add(criteria);
            }
        }

        #endregion


    }
}
